<?php 
/*
Sis skritpas siuncia faila - NETRINK
Reikia ideti apribojima siustis tik registruotiems vartotojams. if (defined("LEVEL")) { true ... }
*/
include_once("priedai/conf.php");



if (isset($url['d']) && isnum($url['d']) && $url['d'] > 0) { $did = (int)$url['d']; } else { $did = 0; }	// download id

# FUNKCIJOS
if (isset($did) && $did >0) {
	$sql = mysql_query("SELECT `siuntiniai`.`file` FROM `siuntiniai` WHERE `siuntiniai`.`ID` = $did LIMIT 0, 1 ");
	if (mysql_affected_rows() > 0) {
		$sql = mysql_fetch_assoc($sql);
		download("downloads/".$sql['file']."",".htaccess|.|..|remontas.php|index.php|config.php");
	}
	else { klaida("Atsiprašome","<h4>Failas neegzistuoja</h4>Prašome apie blogą siuntinį pranešti administracijai"); }
}

function get_user_os() {
  global $global_info, $HTTP_USER_AGENT, $HTTP_SERVER_VARS;
  if (!empty($global_info['user_os'])) {
    return $global_info['user_os'];
  }
  if (!empty($HTTP_SERVER_VARS['HTTP_USER_AGENT'])) {
    $HTTP_USER_AGENT = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
  }
  elseif (getenv("HTTP_USER_AGENT")) {
    $HTTP_USER_AGENT = getenv("HTTP_USER_AGENT");
  }
  elseif (empty($HTTP_USER_AGENT)) {
    $HTTP_USER_AGENT = "";
  }
  if (eregi("Win", $HTTP_USER_AGENT)) {
    $global_info['user_os'] = "WIN";
  }
  elseif (eregi("Mac", $HTTP_USER_AGENT)) {
    $global_info['user_os'] = "MAC";
  }
  else {
    $global_info['user_os'] = "OTHER";
  }
  return $global_info['user_os'];
} 
function get_browser_info() {
  global $global_info, $HTTP_USER_AGENT, $HTTP_SERVER_VARS;
  if (!empty($global_info['browser_agent'])) {
    return $global_info['browser_agent'];
  }
  if (!empty($HTTP_SERVER_VARS['HTTP_USER_AGENT'])) {
    $HTTP_USER_AGENT = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
  }
  elseif (getenv("HTTP_USER_AGENT")) {
    $HTTP_USER_AGENT = getenv("HTTP_USER_AGENT");
  }
  elseif (empty($HTTP_USER_AGENT)) {
    $HTTP_USER_AGENT = "";
  }
  if (eregi("MSIE ([0-9].[0-9]{1,2})", $HTTP_USER_AGENT, $regs)) {
    $global_info['browser_agent'] = "MSIE";
    $global_info['browser_version'] = $regs[1];
  }
  elseif (eregi("Mozilla/([0-9].[0-9]{1,2})", $HTTP_USER_AGENT, $regs)) {
    $global_info['browser_agent'] = "MOZILLA";
    $global_info['browser_version'] = $regs[1];
  }
  elseif (eregi("Opera(/| )([0-9].[0-9]{1,2})", $HTTP_USER_AGENT, $regs)) {
    $global_info['browser_agent'] = "OPERA";
    $global_info['browser_version'] = $regs[2];
  }
  else {
    $global_info['browser_agent'] = "OTHER";
    $global_info['browser_version'] = 0;
  }
  return $global_info['browser_agent'];
} 
//Siunciam nurodyta faila i narsykle. Pratestavau ant visu operaciniu ir narsykliu.
function download($file, $filter) {
    $filter = explode("|", $filter);
    if (!in_array($file, $filter) && is_file($file)) {
		if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) { $file = preg_replace('/\./', '%2e', $file,substr_count($file, '.') - 1);	}
		if (is_file($file)) {
			if(connection_status()==0){
				  if (get_user_os() == "MAC") {
					header("Content-Type: application/x-unknown\n");
					header("Content-Disposition: attachment; filename=\"".basename($file)."\"\n");
				  }
				  elseif (get_browser_info() == "MSIE") {
					$disposition = (!eregi("\.zip$", basename($file))) ? 'attachment' : 'inline';
					header('Content-Description: File Transfer'); 
					header('Content-Type: application/force-download'); 
					header('Content-Length: ' . (string)(filesize($file))); 				
					header("Content-Disposition: $disposition; filename=\"".basename($file)."\"\n");
					header("Cache-Control: cache, must-revalidate");
					header('Pragma: public');
				  }
				  elseif (get_browser_info() == "OPERA") {
					header("Content-Disposition: attachment; filename=\"".basename($file)."\"\n");
					header("Content-Type: application/octetstream\n");
				  }
				  else {
					header("Content-Disposition: attachment; filename=\"".basename($file)."\"\n");
					header("Content-Type: application/octet-stream\n");
				  }
				  header("Content-Length: ".(string)(filesize($file))."\n\n");
				readfile(''.$file.'');
				exit;
			}else{
				header("location: $_SERVER[PHP_SELF]");
				exit;
			}
		}
		else {
			klaida("Failas nerastas","<h4>Atsiprašome</h4>Tačiau pageidaujamas failas nerastas");
			header("HTTP/1.0 404 Not Found");
		}
    }
    else { header("location: ?id,6"); }
}
?>